import member from '@/components/selectMember/selectMember.vue'
import {getUrlParam} from '@/static/js/filter.js'
export default {
	components:{member},
	data () {
		return {
			speciShow: false,
			dialog:false,
			specificTarget:[],
			managePolitic:[],
			foodList:[''],
			healthList: [''],
			exerciseList: [''],
			dangerous:[{
				name:'健康教育处方',
				id:'health',
				data:[]
			},{
				name:'运动标准处方',
				id:'exercise',
				data:[]
			},{
				name:'膳食标准处方',
				id:'food',
				data:[]
			}],
			editIndex:0,
			editData:'',
			objective:'',
			userName:'',
			editId:'',
			showModal:false,
			showContent:'',
			isChoose: true
		}
	},
	mounted(){
		const editId = getUrlParam('id')
		if(editId){
			this.isChoose = false;
			this.editId = editId
			const userId = getUrlParam('userId')
			const userName = decodeURI(getUrlParam('userName'))
			this.userId = userId
			this.userName = userName
			this.$api.InterveneSchemeInfo(editId,userId).then((res)=>{
				this.objective = res.data.result.objective
				this.managePolitic = res.data.result.managePolitic && JSON.parse(res.data.result.managePolitic).map((a,ai)=>{
					const DOM = '#managePolitic'+ai
					setTimeout(()=>{uni.createSelectorQuery().select(DOM).context((c) => {
						this.editorCtx = c.context; this.editorCtx.setContents({html: a.value || ''})}).exec()},200)
					return a
				})
				this.specificTarget = res.data.result.specificTarget && JSON.parse(res.data.result.specificTarget).map((a,ai)=>{
					const DOM = '#specificTarget'+ai
					setTimeout(()=>{uni.createSelectorQuery().select(DOM).context((c) => {this.editorCtx = c.context; this.editorCtx.setContents({html: a.value || ''})}).exec()},200)
					return a
				})
				res.data.result.schemeDetails && res.data.result.schemeDetails.map(a=>{
					this.dangerous[Number(a.schemeName)-1].data.push(a)
				})
			})
		}
	},
	methods:{
		editorInput2(e){
			this.editData[this.editIndex].value = e.detail.html
		},
		hidedialog(){
			this.dialog = false
		},
		delSymptomType(index,data){
			data.splice(index,1)
		},
		// 健康危险因素干预处方新增按钮
		addhealth(e,index){
			e.data.push({schemeName:e.name=='健康教育处方'? 1 : e.name=='运动标准处方'? 2: 3})
		},
		delThis(e,i){
			e.data.splice(i,1)
		},
		onEditorReady() {
			this.dangerous.map((a,ai)=>{
				a.data.map((b,bi)=>{
					const DOM = '#'+a.id+bi
					setTimeout(()=>{uni.createSelectorQuery().select(DOM).context((c) => {
						this.editorCtx = c.context; this.editorCtx.setContents({html: b.noticeMatters})}).exec()}
						,200)
				})
			})
		},
		editorInput(e){
			this.editData[this.editIndex].noticeMatters = e.detail.html
		},
		chooseUser(e){
			this.userId = e.userId;
			this.userName = e.userName;
			this.phone = e.phone
		},
		save(e){
			if(!this.userId){
				uni.showToast({
					title: '请先选择会员',
					icon: 'none'
				})
				return
			}
			var dangerous = []
			this.dangerous.map((a)=>{
				a.data.map((b)=>{
					dangerous.push(b)
				})
			})
			var data = {
				list:dangerous,
				managePolitic:JSON.stringify(this.managePolitic),
				objective:this.objective,
				specificTarget:JSON.stringify(this.specificTarget),
				userId:this.userId,
				realName:this.userName,
				phone:this.phone,
				status:e
			}
			if(this.editId){
				if(dangerous.length == 0){
					data.list = [{"dosage": "","frequency": "","item": "","noticeMatters": "","schemeName": "","time": "","type": ""}]
				}
				this.updateSave(this.editId,data,e)
			}else{
				this.defultsave(data,e);
			}
		},
		// 普通保存
		defultsave(data,e){
			this.$api.InterveneSchemeSave(data).then((res)=>{
				if(res.data.returnCode == 200){
					uni.showToast({
						title: e==1?'保存成功':'签发成功',
						icon: 'none'						
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/quickly/Intervention/list'
						})
					},1000)
				}else{
					uni.showToast({
						title:  res.data.message ,
						icon: 'none'						
					})
				}
			})
		},
		// 编辑保存
		updateSave(editId,data,e){
			this.$api.InterveneSchemeupdate(editId,data).then((res)=>{
				if(res.data.returnCode == 200){
					uni.showToast({
						title:  e==1?'修改成功':'签发成功' ,
						icon: 'none'						
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/quickly/Intervention/list'
						})
					},1000)
				}else{
					uni.showToast({
						title:  res.data.message ,
						icon: 'none'						
					})
				}
			})
		},
		showModalbtn(e){
			if(e==2){
				this.showModal = true
				this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改。'
				return;
			}
			this.save(1)
		},
		Modalconfirm(){
			this.save(2)
		}
	}
}